/**
 * Mixin AjaxRequest
 * 
 * Used to "mixin" with other classes.
 * Provides methods for calling action methods
 * 
 * @author David Lindkvist (david.lindkvist@shapeshift.se)
 */

//define a mixin module
PWF.mixin.AjaxRequest = {

	callAction: function(/*String*/actionUrl, /*String*/method) 
	{
		//call init method
		this.onRequestInit();
	
		//set method or use default
		if(method === undefined || method === null)
			method = 'post';
	
		var debugParams = this.actionParams ? this.actionParams.toQueryString() : '';
		Log.debug('callAction: '+actionUrl+' ('+method+') params:'+debugParams);
		
		//launch ajax request
		new Ajax.Request(this.actionUrl, {
		  method: method,
		  parameters: this.actionParams,
		  requestHeaders: {Accept: 'application/json'},
		  onSuccess: this.onRequestSuccess.bind(this),
		  onFailure: this.onRequestFailure.bind(this),
		  onException: this.onRequestException.bind(this)
		});
	},
	onRequestInit: function() 
	{	
		//override me pls!
	},
	onRequestSuccess: function(/*Object*/transport) 
	{	
		//override me pls!		
	},
	onRequestFailure: function(/*Object*/transport) 
	{
		//override me pls!
	},
	onRequestException: function(/*Object*/request) 
	{
		//override me pls!
	},
	addParameter: function(/*String*/key, /*String*/value) 
	{
		//create property if not defined
		if(this.actionParams === undefined || this.actionParams === null)
		{
			this.actionParams = new Hash();
		}		
		this.actionParams.set(key, value);
	},
	clearParameters: function() 
	{
		this.actionParams = null;
	}
};